/**
 * Agent相关的TypeScript类型定义
 * 从ai-writer-lite项目迁移
 */

import { BaseMessage } from "@langchain/core/messages";
import { RunnableConfig } from "@langchain/core/runnables";

// 基础Agent状态
export interface AgentState {
  messages: BaseMessage[];
  currentStep: string;
  isProcessing: boolean;
  error?: string;
  metadata?: Record<string, any>;
}

// Agent执行结果
export interface AgentResult {
  success: boolean;
  result?: string;
  nextAction?: string;
  toolCalls?: ToolCall[];
  metadata?: Record<string, any>;
  error?: string;
}

// 工具调用信息
export interface ToolCall {
  id: string;
  toolName: string;
  arguments: Record<string, any>;
  result?: any;
  status: 'pending' | 'success' | 'error';
  error?: string;
}

// Agent配置
export interface AgentConfig {
  name: string;
  description: string;
  systemPrompt: string;
  temperature?: number;
  maxTokens?: number;
  tools: string[];
  memory?: MemoryConfig;
  model?: string;
  provider?: string;
}

// 内存配置
export interface MemoryConfig {
  conversationalMemory: {
    maxMessages: number;
    returnMessages: boolean;
  };
  vectorStoreMemory: {
    enabled: boolean;
    vectorStore: 'chroma' | 'faiss' | 'pinecone';
    embeddingModel: string;
    collectionName: string;
  };
  episodicMemory: {
    enabled: boolean;
    maxEpisodes: number;
  };
}

// Agent能力定义
export interface AgentCapability {
  name: string;
  description: string;
  tools: string[];
  prompts: string[];
  enabled: boolean;
}

// 写作任务类型
export type WritingTaskType =
  | 'generate'
  | 'polish'
  | 'expand'
  | 'summarize'
  | 'analyze'
  | 'continue'
  | 'outline'
  | 'review'
  | 'research';

// 写作上下文
export interface WritingContext {
  projectId: string;
  chapterId?: string;
  selectedText?: string;
  surroundingText?: string;
  chapterSummary?: string;
  projectOverview?: string;
  userPreferences?: Record<string, any>;
  writingHistory?: WritingHistoryEntry[];
}

// 写作历史条目
export interface WritingHistoryEntry {
  timestamp: Date;
  action: WritingTaskType;
  input: string;
  output: string;
  toolUsed: string;
  duration: number;
  userRating?: number;
}

// Agent思考过程
export interface AgentThought {
  step: number;
  description: string;
  reasoning: string;
  action: string;
  observation?: string;
  confidence: number;
}

// Agent执行计划
export interface AgentExecutionPlan {
  goal: string;
  steps: AgentExecutionStep[];
  estimatedDuration: number;
  requiredTools: string[];
}

// Agent执行步骤
export interface AgentExecutionStep {
  id: string;
  description: string;
  toolName: string;
  arguments: Record<string, any>;
  dependencies: string[];
  status: 'pending' | 'in_progress' | 'completed' | 'failed';
  result?: any;
  error?: string;
}

// LangChain运行配置
export interface LangChainConfig extends RunnableConfig {
  agentName: string;
  context: WritingContext;
  capabilities: AgentCapability[];
  onProgress?: (progress: AgentProgress) => void;
  onComplete?: (result: AgentResult) => void;
  onError?: (error: Error) => void;
}

// Agent进度信息
export interface AgentProgress {
  step: number;
  totalSteps: number;
  currentAction: string;
  thought?: AgentThought;
  toolCall?: ToolCall;
  message?: string;
}

// Agent状态变更
export type AgentStateChange =
  | { type: 'START_PROCESSING' }
  | { type: 'STOP_PROCESSING' }
  | { type: 'ADD_MESSAGE'; payload: BaseMessage }
  | { type: 'SET_ERROR'; payload: string }
  | { type: 'UPDATE_STEP'; payload: string }
  | { type: 'UPDATE_METADATA'; payload: Record<string, any> };

// Agent事件类型
export type AgentEventType =
  | 'agent:start'
  | 'agent:end'
  | 'agent:error'
  | 'tool:start'
  | 'tool:end'
  | 'tool:error'
  | 'thought:generated'
  | 'plan:created';

// Agent事件数据
export interface AgentEvent {
  type: AgentEventType;
  timestamp: Date;
  agentName: string;
  data?: any;
  error?: Error;
}

// 导出所有类型
export type {
  AgentState,
  AgentResult,
  ToolCall,
  AgentConfig,
  MemoryConfig,
  AgentCapability,
  WritingTaskType,
  WritingContext,
  WritingHistoryEntry,
  AgentThought,
  AgentExecutionPlan,
  AgentExecutionStep,
  LangChainConfig,
  AgentProgress,
  AgentStateChange,
  AgentEvent,
};